import java.sql.*;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Logger;


import static java.sql.DriverManager.getConnection;
//增加数据库
public class jdbcdemo02 {
    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        Statement stmt = null;
        try{
            //1.注册驱动

            Driver driver = new Driver();//多态，父类型引用指向子类型对象
            //Driver driver = new Oracle.jdbc.driver.OracleDriver(); // orcale的驱动
            DriverManager.registerDriver(driver);
            //2.获取连接
            /*
                url:统一资源定位符(网站中某个资源的绝对路径)
                localhost和127.0.0.1 都是本机ip地址
            */
            String url = "jdbc:mysql://localhost:3306/text";
            String user = "root";
            String password = "123456";
            conn = DriverManager.getConnection(url,user,password);
            //连接对象地址 com.mysql.cj.jdbc.ConnectionImpl@42607a4f
            System.out.println("数据库连接对象" + conn);
            //3.获取数据库操作对象 （Statement专门执行SQL语句）
            stmt = conn.createStatement();
            //4.执行SQL
           String sql = "insert into text_detail(text_id,text_name) values(3,'王五')";
            int count  = stmt.executeUpdate(sql);
            System.out.println(count == 1 ? "保存成功":"保存失败");
            }catch(SQLException e) {
                e.printStackTrace();
        }finally {
            //释放资源
            //为了保证资源在finally语句块中关闭资源 遵信从小到大依次关闭
            try{
                if (stmt != null) {
                    stmt.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
            try{
                if (conn != null) {
                    conn.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }

    private static class Driver implements java.sql.Driver {
        public Connection connect(String url, Properties info) throws SQLException {
            return null;
        }

        public boolean acceptsURL(String url) throws SQLException {
            return false;
        }

        public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
            return new DriverPropertyInfo[0];
        }

        public int getMajorVersion() {
            return 0;
        }

        public int getMinorVersion() {
            return 0;
        }

        public boolean jdbcCompliant() {
            return false;
        }

        public Logger getParentLogger() throws SQLFeatureNotSupportedException {
            return null;
        }
    }
}
